package com.example.aidocengine.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

public class ValidationUtils {
    
    // 设备双编验证正则表达式
    private static final Pattern DEVICE_CODE_PATTERN = Pattern.compile("^[A-Za-z0-9\\u4e00-\\u9fa5_\\-（）()]+$");
    
    // 受令单位名称验证正则表达式
    private static final Pattern UNIT_NAME_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5A-Za-z0-9_\\-（）()]+$");

    public static boolean isValidDeviceCode(String deviceCode) {
        if (StringUtils.isBlank(deviceCode)) {
            return false;
        }
        return DEVICE_CODE_PATTERN.matcher(deviceCode.trim()).matches();
    }

    public static boolean isValidUnitName(String unitName) {
        if (StringUtils.isBlank(unitName)) {
            return false;
        }
        return UNIT_NAME_PATTERN.matcher(unitName.trim()).matches();
    }

    public static boolean isNotEmpty(String str) {
        return StringUtils.isNotBlank(str);
    }

    public static String trimToEmpty(String str) {
        return StringUtils.trimToEmpty(str);
    }

    /**
     * 检查操作步骤中是否包含黑色括号内容
     */
    public static boolean containsBlackBrackets(String operationStep) {
        if (StringUtils.isBlank(operationStep)) {
            return false;
        }
        // 检查是否包含中文括号或英文括号
        return operationStep.contains("（") && operationStep.contains("）") ||
               operationStep.contains("(") && operationStep.contains(")");
    }

    /**
     * 提取黑色括号内的内容
     */
    public static String extractContentFromBrackets(String operationStep) {
        if (StringUtils.isBlank(operationStep)) {
            return "";
        }
        
        // 优先匹配中文括号
        int start = operationStep.indexOf("（");
        int end = operationStep.indexOf("）");
        
        if (start != -1 && end != -1 && start < end) {
            return operationStep.substring(start + 1, end).trim();
        }
        
        // 其次匹配英文括号
        start = operationStep.indexOf("(");
        end = operationStep.indexOf(")");
        
        if (start != -1 && end != -1 && start < end) {
            return operationStep.substring(start + 1, end).trim();
        }
        
        return "";
    }
}